package com.zby.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zby.admin.pojo.dto.CategoryTypeDto;
import com.zby.admin.pojo.vo.CategoryTypeVo;
import com.zby.admin.pojo.vo.GetCategoryTypeVo;
import com.zby.admin.service.CategoryTypeService;
import com.zby.utils.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class CategoryTypeController {
    @Resource
    private CategoryTypeService categoryTypeService;

    /**
     * 插入商品分类类型数据
     */
    @PostMapping("/add")
    public Result<Integer> addCategoryType(@RequestBody CategoryTypeDto categoryTypeDto, HttpServletRequest request) {
        boolean result = categoryTypeService.insert(categoryTypeDto, request);
        if (result) {
            return Result.success();
        }
        return Result.error(500, "添加失败");
    }

    /**
     * 修改商品分类类型数据
     */
    @PutMapping("/edit")
    public Result<Integer> editCategoryType(@RequestBody CategoryTypeDto categoryTypeDto, HttpServletRequest request) {
        boolean result = categoryTypeService.updateCategoryType(categoryTypeDto, request);
        if (result) {
            return Result.success();
        }
        return Result.error(500, "修改失败");
    }

    /**
     * 获取商品分类类型数据
     */
    @GetMapping("/list")
    public Result<Page<CategoryTypeVo>> getCategoryTypeList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<CategoryTypeVo> response = categoryTypeService.getCategoryTypeList(page, pageSize);
        return Result.success(response);
    }

    /**
     * todo 获取商品分类类型数据
     */
    @GetMapping("/listType")
    public Result<List<GetCategoryTypeVo>> getCategoryType() {
        List<GetCategoryTypeVo> response = categoryTypeService.getCategoryType();
        return Result.success(response);
    }

    /**
     * 删除商品分类类型数据
     */
    @DeleteMapping("/delete")
    public Result<Integer> deleteCategoryType(@RequestParam Integer id) {
        boolean result = categoryTypeService.removeById(id);
        if (result) {
            return Result.success();
        }
        return Result.error(500, "删除失败");
    }
}
